package string;

/**
 * String当中与转换相关的常用方法有：
 * public char[] toCharArray():将当前字符串拆分称为字符数组作为返回值
 * public byte[] getBytes():获得当前字符串底层的字节数组
 * public String replace(CharSequence oldString, CharSequence newString):将左右出现的老字符串替换称为新的字符串
 */
public class Demo02StringConvert {

    public static void main(String[] args) {
        // 转换为字符数组
        char[] chars = "Hello".toCharArray();
        System.out.println(chars[0]);     // H
        System.out.println(chars.length); // 5
        System.out.println("==================");

        // 转换为字节数组
        byte[] bytes = "abc".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
        System.out.println("==================");

        String str1 = "How do you do?";
        String str2 = str1.replace("o", "*");
        System.out.println(str1); // How do you do?
        System.out.println(str2); // H*w d* y*u d*?
        System.out.println("==================");

        // 游戏中敏感词汇替换
        String lang1 = "会不会玩啊！你大爷的！";
        String lang2 = lang1.replace("你大爷的", "****");
        System.out.println(lang2); // 会不会玩啊！****！
    }
}
